package CA;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import junit.framework.TestCase;

public class InjectBugs_Test extends TestCase {

	protected void setUp() throws Exception {
		super.setUp();
	}

	protected void tearDown() throws Exception {
		super.tearDown();
	}

	
	public void testRemoveFirstCrashInfoFromLine(){
		InjectBugs ib = new InjectBugs();
		final String CrashInfo1="msvcr80.dll^*^_threadstartex^*^f:\\dd\\vctools\\crt_bld\\self_x86\\crt\\src\\threadex.c:326";
		final String CrashInfo2="10xul.dll^*^nsThread::ProcessNextEvent^*^xpcom/threads/nsThread.cpp:631";
		final String CrashInfo3="xul.dll^*^nsThreadStartupEvent::Run^*^xpcom/threads/nsThread.cpp:201";
		final String CrashInfo4="nspr4.dll^*^_PR_NativeRunThread^*^nsprpub/pr/src/threads/combined/pruthr.c:426";
		final String CrashInfo5="nspr4.dll^*^pr_root^*^nsprpub/pr/src/md/windows/w95thred.c:122";
		final String CrashInfo6="msvcr80.dll^*^_callthreadstartex^*^f:\\dd\\vctools\\crt_bld\\self_x86\\crt\\src\\threadex.c:348";
		final String CrashInfo7="^*^_threadstartex^*^f:\\dd\\vctools\\crt_bld\\self_x86\\crt\\src\\threadex.c:326";
		
		// test basic removals
		assertEquals(ib.removeFirstCrashInfoFromLine(CrashInfo1),"^*^_threadstartex^*^f:\\dd\\vctools\\crt_bld\\self_x86\\crt\\src\\threadex.c:326");
		assertEquals(ib.removeFirstCrashInfoFromLine(CrashInfo2),"^*^nsThread::ProcessNextEvent^*^xpcom/threads/nsThread.cpp:631");
		assertEquals(ib.removeFirstCrashInfoFromLine(CrashInfo3),"^*^nsThreadStartupEvent::Run^*^xpcom/threads/nsThread.cpp:201");
		assertEquals(ib.removeFirstCrashInfoFromLine(CrashInfo4),"^*^_PR_NativeRunThread^*^nsprpub/pr/src/threads/combined/pruthr.c:426");
		assertEquals(ib.removeFirstCrashInfoFromLine(CrashInfo5),"^*^pr_root^*^nsprpub/pr/src/md/windows/w95thred.c:122");
		assertEquals(ib.removeFirstCrashInfoFromLine(CrashInfo6),"^*^_callthreadstartex^*^f:\\dd\\vctools\\crt_bld\\self_x86\\crt\\src\\threadex.c:348");
		
		// Do nothing since the first item is already removed
		assertEquals(ib.removeFirstCrashInfoFromLine(CrashInfo7),"^*^_threadstartex^*^f:\\dd\\vctools\\crt_bld\\self_x86\\crt\\src\\threadex.c:326");

		
	}
	
	
	public void testInsertMissingInformation() throws IOException{
		
		FileCleaning fc = new FileCleaning();
		InjectBugs ib = new InjectBugs();
		Helper h = new Helper();
		CrashSuper cs = new CrashSuper();
		
		// Build everything. Set up everything by default in order to have a clean slate 		
		final String MainExaminLocation = System.getProperty("user.dir") + "/UnitTestData/";
		final String compareFile = MainExaminLocation+"/missingStack/MissingStackFolder5/";
		File parentCopy = new File(compareFile+"/backupnoscan/");
		File childCopy1 = new File(compareFile+"");
		fc.copyFolder(parentCopy, childCopy1);
		
		List<FileInfo> fileList = new ArrayList<FileInfo>();
		fileList=cs.getFileList(compareFile); // Build the list of file objects to be examined
		cs.BuildFileData(fileList, compareFile, false); // Add the values to these objects
		
		
		List<String> AllFiles=new ArrayList<String>();	
		AllFiles=h.getAllFileNames(compareFile);
	
		assertEquals(AllFiles.size(),22);
		assertEquals(h.getTotalNumberOfFolders(fileList,true),1);
		assertEquals(h.getTotalNumberOfFolders(fileList,false),4);
	//	assertEquals(h.getTotalNumberOfFolders(fileList,false),h.getTotalNumberOfFolders(fileList,true));
	
		assertEquals(h.getTotalNumberOfFoldersWithMissingCrashInfo(fileList,true),0);
		assertEquals(h.getTotalNumberOfFoldersWithMissingCrashInfo(fileList,false),3);
	//	assertEquals(h.getTotalNumberOfFoldersWithMissingCrashInfo(fileList,false),h.getTotalNumberOfFoldersWithMissingCrashInfo(fileList,true));
	
		assertEquals(h.getTotalLinesMissingCrashInfoInAllFiles(fileList,true),0);
		assertEquals(h.getTotalLinesMissingCrashInfoInAllFiles(fileList,false),15);
	//	assertEquals(h.getTotalLinesMissingCrashInfoInAllFiles(fileList,false),h.getTotalLinesMissingCrashInfoInAllFiles(fileList,true));
		
		
		// Now run script delete data - for testing purposes set to 5
		ib.InsertMissingInformation(compareFile, 5);
		
		// check the results
		List<FileInfo> fileList2 = new ArrayList<FileInfo>();
		fileList2=cs.getFileList(compareFile); // Build the list of file objects to be examined
		cs.BuildFileData(fileList2, compareFile, false); // Add the values to these objects
		
		List<String> AllFiles2=new ArrayList<String>();	
		AllFiles2=h.getAllFileNames(compareFile);
		assertEquals(AllFiles2.size(),22);
			
		assertEquals(h.getTotalNumberOfFolders(fileList2,true),0);
		assertEquals(h.getTotalNumberOfFolders(fileList2,false),4);
	//	assertEquals(h.getTotalNumberOfFolders(fileList2,false),h.getTotalNumberOfFolders(fileList2,true));

		assertEquals(h.getTotalNumberOfFoldersWithMissingCrashInfo(fileList2,true),0);
		assertEquals(h.getTotalNumberOfFoldersWithMissingCrashInfo(fileList2,false),4);
	//	assertEquals(h.getTotalNumberOfFoldersWithMissingCrashInfo(fileList2,false),h.getTotalNumberOfFoldersWithMissingCrashInfo(fileList2,true));
	
		assertEquals(h.getTotalLinesMissingCrashInfoInAllFiles(fileList2,true),0);
		assertEquals(h.getTotalLinesMissingCrashInfoInAllFiles(fileList2,false),150);
	//	assertEquals(h.getTotalLinesMissingCrashInfoInAllFiles(fileList2,false),h.getTotalLinesMissingCrashInfoInAllFiles(fileList2,true));
				
		// Total Line Count: 746. Inject fault count: 149 or 150
		
	}
	
	
	
	
}
